<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      background-color:red;
    }
  </style>
</head>
<body>
  <div id="app">

    <router-link tag="button" to="/home">
      首页
    </router-link>
    <router-link tag="button"  to="/news/1">
      新闻页
    </router-link>
    <router-link tag="button" to="/about">
      关于我们
    </router-link>
    <hr>
    <router-view/> 
    <!-- 路由组件的出口 -->
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    /* 
    引入vue-router
    1,在vue.js后面引入
    2，往 Vue构造函数上 加了点东西
    让实例上增加了俩属性
    $route  保存了当前路由的所有信息 （用于获取路由跳转传参数）
    */
   const Home = {
     template: `
      <div>
        home页  
      </div>
     `
   }
   const About = {
     template: `
      <div>
        关于我们  
      </div>
     `
   }
   const News = {
     template: `
      <div>
        新闻页  
        {{ $route.params.newId }}
      </div>
     `,
     mounted(){
       console.log(this)
       console.log(this.$route.params.newId)
     },
     watch:{ // 监听动态路由的变化
      $route(to, from){
        console.log('数据变化了')
        console.log(to)
        console.log(from)
      }
     }
   }
  
  const routes = [ // 配置路由规则
      {
        path: '/home',
        component: Home
      },
      {
        path: '/news/:newId',
        component: News
      },
      {
        path: '/about',
        component: About
      }
    ]
  //  定义路由
  const router = new VueRouter({
    routes
  })
    const vm = new Vue({
      el: '#app',
      router
    })

  </script>
</body>
</html>